package com.vchello.controller.admin;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.vchello.bean.file.Folder;
import com.vchello.bean.security.Role;
import com.vchello.bean.user.User;
import com.vchello.bean.user.UserProgress;
import com.vchello.controller.BaseController;
import com.vchello.service.file.FolderService;
import com.vchello.service.project.ProjectService;
import com.vchello.service.security.RoleService;
import com.vchello.service.user.UserService;
import com.vchello.util.Utility;

/**
 * 
 * @author power
 *
 */
@Controller
@RequestMapping(value = "/admin/user")
public class UserAdminController extends BaseController {
	
	private static final int[] STATUS = {1, 2};
	private static final int[] TYPE = {1, 2, 4}; //1：机构 2：投资人 4:管理员
	
	@Autowired
	private UserService userService;
	@Autowired
	private RoleService roleService;
	@Autowired
	private FolderService folderService;
	@Autowired
	private ProjectService projectService;

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(Page<User> pages, User user, Model model){
		pages = userService.selectPage(pages, new EntityWrapper<User>(user));
		model.addAttribute("pages", pages);
		return "admin/user/userList";
		
	}
	
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	public String listAjax(Page<User> pages, User user, Model model){
		pages = userService.selectPage(pages, new EntityWrapper<User>(user));
		model.addAttribute("pages", pages);
		return "admin/user/query/userQuery";
		
	}
	
	@RequestMapping(value = "/addUser", method = RequestMethod.GET)
	public String addUser(Model model){
		return "admin/user/add";
		
	}
	
	@RequestMapping(value = "/addUser", method = RequestMethod.POST)
	public void addUser(User user){
		if(null == user){
			error("用户信息为空");
			return;
		}
		User isExistUser = userService.findUserByPhone(user.getPhone());
		if(null != isExistUser) {
			error("手机号码已存在");
			return;
		}
		user.setPassword(Utility.md5md5(user.getPassword()));
		user.setCreateTime(Utility.getSimpleFormatedDayNow());
		user.setStatus(STATUS[0]);
		boolean isTrue = userService.insert(user);
		if(!isTrue) {
			error("新增用户失败");
			return;
		}
		if(user.getType() != TYPE[2]) {
			if(user.getType() == TYPE[0]) {
				roleService.saveRole(new Role("合伙人", user.getId()));    //帮用户创建默认的城市合伙人角色
			} else if(user.getType() == TYPE[1]) {
				projectService.saveOrUpdateProgress(new UserProgress("新收到", projectService.queryProgressSort(user.getId()), (long)user.getId(), 1));//为用户创建默认进度“新收到”
			}
			folderService.saveFolder(new Folder("我的文件", user.getId(), 0, 0, Utility.getSimpleFormatedDayNow()));  //帮用户创建默认文件夹(不能删除)
			folderService.saveFolder(new Folder("项目文件", user.getId(), 0, 0, Utility.getSimpleFormatedDayNow()));
		}
		success("新增用户成功");
	}
	
	@RequestMapping(value = "/edit/{userId}", method = RequestMethod.GET)
	public String edit(Model model, @PathVariable int userId){
		User user = userService.findByOne(userId);
		model.addAttribute("user", user);
		return "admin/user/edit";
		
	}
	
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	public void edit(User user){
		User oldUser = userService.findByOne(user.getId());
		if(null == oldUser){
			error("用户信息为空");
			return;
		}
		oldUser.setPhone(user.getPhone());
		oldUser.setEmail(user.getEmail());
		oldUser.setUpdateTime(Utility.getSimpleFormatedDayNow());
		boolean isTrue = userService.insertOrUpdate(user);
		if(isTrue) {
			success("修改用户信息成功");
			return;
		}
		error("修改用户信息失败");
	}
	
	@RequestMapping(value = "/resetPassword", method = RequestMethod.POST)
	public void resetPassword(int userId){
		User user = userService.findByOne(userId);
		user.setPassword(Utility.md5md5("123456"));
		boolean isTrue = userService.insertOrUpdate(user);
		if(isTrue) {
			success("重置密码成功");
			return;
		}
		error("重置密码失败");
	}
	
	@RequestMapping(value = "/lockingUser", method = RequestMethod.POST)
	public void lockingUser(int userId){
		User user = userService.findByOne(userId);
		if(null == user) {
			error("找不到当前用户");
			return;
		}
		user.setStatus(STATUS[1]);
		boolean isTrue = userService.insertOrUpdate(user);
		if(isTrue) {
			success("锁定用户成功");
			return;
		}
		error("锁定用户失败");
	}
	
	@RequestMapping(value = "/unbindWeChat", method = RequestMethod.POST)
	public void unbindWeChat(int userId){
		User user = userService.findByOne(userId);
		if(null == user) {
			error("找不到当前用户");
			return;
		}
		boolean isTrue = userService.insertOrUpdate(user);
		if(isTrue) {
			success("解除微信成功");
			return;
		}
		error("锁定微信失败");
	}
}
